<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script type="text/javascript" src="../../js/jquery-3.1.0.min.js"></script>
<style type="text/css">
.goods{background: threedlightshadow;}
.title{background-color:gray;left:400pt }
.shop{width: 450pt;height: 1000pt;};
</style>
<title>商品列表</title>
<script>	
	$(document).ready(function() {
		var totalpage=1;
		var currentpage="${empty param.currentpage?'1':param.currentpage}";
			getList();
						$("#firest").click(function(){
							clear();
							currentpage=1;
							getList();
						})
						$("#up").click(function(){
							if(currentpage==1){
								alert("当前已经为第一页了");
							}else{
							clear();
								currentpage--;
								getList();
							}
						});
						$("#down").click(function(){
							if(currentpage==totalpage){
								alert("当前页已经是最后一页");
							}else{
							clear();
								currentpage++;
								getList();
							}
						});
							$("#end").click(function(){
								clear();
								currentpage=totalpage;
								getList();
							})
							$(".add").click(function(){
								location.href="GetListGoodsTypeServlet";
							})
							$(".search").click(function(){
								currentpage=1;
								clear();
								getList();
							});
						function getList(){
							$.get("GetGoodsListServlet?currentpage="+currentpage+"&namekey="+$("#namekey").val(),function(data, status) {
								if (status == "success") {
									var glist = JSON.parse(data);
									currentpage = glist[glist.length - 1];
									totalpage = glist[glist.length - 2];
									$("#page").html(currentpage+"/"+totalpage);
									for (var i = 0; i < glist.length - 2; i++) {
										var status = glist[i].gShelves==1?"上架":"下架";
										var st = glist[i].gShelves==1?"下架":"上架";
										$(".goods").append($("<tr><td class='id'>" +glist[i].gid+ "</td><td class='gName'>"+ glist[i].gName
														+ "</td><td class = 'gPrice'>"+ glist[i].gPrice+ "</td><td class='gAccount'>"+ glist[i].gAccount
														+ "</td><td >"+ glist[i].goodsType.goodstype_name+ "</td><td class='gShelves' value="+glist[i].gShelves+">"+status+"</td><td><input type = 'button' value = '删除' class ='del'>"+
														"<input type = 'button' value = '修改' class='update'><input type = 'button' class='change' value='"+st+"'></td></tr>"));
									}
									$(".del").click(function(){
										var id = $(this).parents("td").prevAll(".id").html();
										if(confirm("是否确定删除")){
											location.href="delGoodsServlet?gid="+id+"&currentpage="+currentpage;
										}
									});
								  $(".update").click(function(){
									  alert($(this).parents("td").prevAll(".gName").html());
									  location.href ="GetListGoodsTypeServlet?gName="+$(this).parents("td").prevAll(".gName").html()+"&gPrice="+$(this).parents("td").prevAll(".gPrice").html()+"&gAccount="+$(this).parents("td").prevAll(".gAccount").html()+"&id="+$(this).parents("td").prevAll(".id").html();
									})
									$(".change").click(function(){
											location.href="UpdateGoodsStatusServlet?id="+$(this).parents("td").prevAll(".id").html()+"&gShelves="+$(this).parents("td").prevAll(".gShelves").attr("value");
									})
								}
							})		
			         }
						function clear(){
							$(".goods").empty("tr");
							$(".goods").append($("<tr><th>商品id</th><th>商品名称</th><th>商品价格</th><th>商品库存</th><th>商品类型名称</th><th>状态</th><th>操作</th></tr>"));
						}				
			});
</script>
</head>
<body>
<body>
<div class="shop" align="center">
 <h1>欢迎进入${user.userName}的店铺</h1> 
 <div align="left" class="title">
商品名称：<input type="text" id="namekey" >
<input type="button" value="搜索" class="search">
<input type="button" value="添加" class="add">
 </div>
	<table border="1" class="goods">
		<tr>
			<th>商品id</th>
			<th>商品名称</th>
			<th>商品价格</th>
			<th>商品库存</th>
			<th>商品类型名称</th>
			<th>上架状态</th>
			<th>操作</th>
		</tr>
	</table>
	<input type="button" value="第一页" id= "firest">
	<input type="button" value="上一页" id ="up">
	<input type="button" value="下一页" id = "down">
	<input type="button" value="最后一页" id = "end">
	<span id="page"></span>
	</div>
</body>
</html>